package fuse.LaCollaFS.Api;

import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import LaColla.core.util.constant;
import LaColla.Api.Api;
import LaColla.core.data.TimestampSummary;

//this class is instancied by aplication to publish the services offered to LaCOLLA

public class ApplicationsSideApiServer{


private String rmiHost;
private int aplicPort;
//private ApplicationsSideApiImpl aplicapi=null;
private ApplicationsSideApiBase aplicapi=null;
private String memberId;
private Registry registry;
private String aplicuri;


//constructor
public ApplicationsSideApiServer(String rmihost,int aplicPort) {
   
   this.rmiHost=rmihost;
   this.aplicPort=aplicPort;
 
   // Create and install a security manager
	//System.setSecurityManager(new RMISecurityManager());

   try {
    
    aplicapi = new ApplicationsSideApiBase();
    System.out.println("creada instancia de AplicationsSideApi");
    System.out.println("rmiport="+constant.RMIPort);
    registry = LocateRegistry.getRegistry(rmiHost,constant.RMIPort);
    aplicuri=constant.DEFAULT_APSAPI_LOCATION+aplicPort;
    registry.rebind(aplicuri, aplicapi);
     
    
    System.out.println("name published "+aplicuri);
     
   } catch (Exception e) { System.out.println("Problema: " + e);
   							e.printStackTrace();
    }	
  }

//explicit destructor. Must be called by application.
  public void close() {
	  aplicapi.close();
  }

  /** 
   * This method returns an stub of an LaCOLLA Api object.
   * It assumes that port of the rmiregistry is 1099, and the host is set 
   * by the constructor.  
   *  
   * @param apiPort  The LaCOLLA Api port 
   *
   * @return The LaCOLLA Api object
   */
  public Api resolveApi(int apiPort) {
	  Api api=null;
	  try {
		Registry registry = LocateRegistry.getRegistry(rmiHost,constant.RMIPort);
		String api_uri=constant.DEFAULT_API_LOCATION+apiPort;
		api= (Api)registry.lookup(api_uri);
	  } catch(Exception e){ System.out.println("Problema: " + e);
			e.printStackTrace();
	  }
	  return api;
  }

  public void initialize(Object obj){
	try{
	aplicapi.initialize(obj);
	
/* ejaen
	Registry registry = LocateRegistry.getRegistry(host,port);	
	registry.rebind("/AplicationsSideApi", aplicapi);
	
//	Naming.rebind("//"+host+":"+port+ "/AplicationsSideApi", aplicapi);
	System.out.println("Api Initialized: ");
*/	
  	} catch (Exception e) { System.out.println("Problema: " + e);
   		e.printStackTrace();
    }
    	
  }

  
  public TimestampSummary readSummary() {
	  return aplicapi.readSummary();
  }

/* TODO: preguntar si s'usa
  public void setId(String Id){
  	try{
  	aplicapi.setAplicationId(Id);
  	Registry registry = LocateRegistry.getRegistry(host,port);	
  	registry.rebind("/AplicationsSideApi", aplicapi);
//  	Naming.rebind("//"+host+":"+port+ "/AplicationsSideApi", aplicapi);
	}
	 catch (Exception e) { System.out.println("Problema: " + e);
   							e.printStackTrace();
    }	
  }
*/
  
/*
public void publish(){
   
   try {
    aplicapi = new AplicationsSideApiImpl();
    System.out.println("AplicationsSideApi Instatied ");
    Naming.rebind("//"+host+":"+port+ "/AplicationsSideApi", aplicapi);
    System.out.println("name published: ");
   } 
   catch (Exception e) { 
   	System.out.println("Problema: " + e);
   	e.printStackTrace();
   }
}
*/


public void unpublish(){
	
  try {
  	registry.unbind(aplicuri);
  	
    System.out.println("name UNpublished: ");
   } 
   catch (Exception e) { 
   	System.out.println("Problema: " + e);
   	e.printStackTrace();
   }
}


/**
 * @return
 */
public String getMemberId() {
	String memberId=null;
	try{
		memberId=aplicapi.getMemberId();
		
	}
	catch (Exception e) { System.out.println("Problema: " + e);
	e.printStackTrace();
	}	
	return memberId;
}

public ApplicationsSideApiImpl getASAI() { 
	return aplicapi.getASAI();
}

}